<?php
namespace Api\Controller;
use Api\Traits\Weixin;

/**
 * author: Administrator
 * Createtime: 2015/10/2613:53
 * description: 接口
 */

class AppController extends AppBaseController
{
    use Weixin;



    /*岗位查询接口
     *@ j_drivedate 驾龄
     *@ j_job_form 司机类型
     *@ j_workplace 工作地点
     * @j_payscope 薪资待遇
     * @ addtimebw 近几天
     * return json
     */
    public function jobsearch()
    {
        header("Content-type: text/html; charset=utf-8");
        $post = I('post.');
        $post['page'] = $post['page'] ? $post['page'] : 1;
        $post['size'] = $post['size'] ? $post['size'] : 10;

        $where['islock'] = 1;//公开
        if ($post['j_drivedate'])
            $where['j_drivedate'] = $post['j_drivedate'];
        if ($post['j_job_form'])
            $where['j_job_form'] = $post['j_job_form'];
        if ($post['j_workplace']){
            $post['j_workplace'] = intval($post['j_workplace']);
            $arrchildid = M('linkage')->where(['linkageid'=>$post['j_workplace']])->getField('arrchildid');
            $where['j_workplace'] = ['in', $arrchildid];
        }

        if ($post['j_payscope'])
            $where['j_payscope'] = $post['j_payscope'];
        if ($post['addtimebw']) {
            $where['addtimebw'] = ['gt',time()-$post['addtimebw'] * 24 * 3600];
        }

        $model = M('member_firmdata_job');

        $count = $model->where($where)->count();
        $page = $this->page($count, $post['size'],$post['page'] );
        $lists = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order(['updatedate'=>'desc'])->select();
        foreach ($lists as $key=>$value){
            $list[$key] = $value;
            $list[$key]['companyname'] = M('member_company')->where(['userid'=>$value['userid']])->getField('f_name');
            $list[$key]['j_job_form'] = category_post($value['j_job_form']);
            $list[$key]['j_workplace'] = city($value['j_workplace']);
            $list[$key]['j_job_type'] = category_jobs($value['j_job_type']);
            $list[$key]['j_education'] = category_jobs($value['j_education']);
            $list[$key]['j_drivedate'] = category_jobs($value['j_drivedate']);
            $list[$key]['j_payscope'] = category_jobs($value['j_payscope']);
            $list[$key]['j_drive'] = category_jobs($value['j_drive']);
        }
//        echo $page->show();
        $data['list'] = $list;
        $data['count'] = $count;
        $this->message("success", [
            'error' => 10000,
            'data' => $data,
        ]);
    }

    /*司机用户登录*/
    //验证登陆
    public function login() {
        //用户名
        $loginName = I('request.username', null, 'trim');
        //密码
        $password = I('request.password');

        if (empty($loginName)) {
            $this->message(10005, array(), 'error');
        }
        if (empty($password)) {
            $this->message(20023, array(), 'error');
        }
        $modelid = I('request.modelid','0','intval');

        $cookieTime = '';
        $userid = service('Passport')->loginLocal($loginName, $password, $cookieTime ? 86400 * 180 : 86400);
        if ($userid > 0) {
            $userInfo = service("Passport")->getLocalUser((int) $userid);

            //判断用户类型是否正确
            if($modelid != $userInfo['modelid']) {
                $this->message('用户类型错误', array(), 'error');
            }

            //邮箱验证
            if($userInfo['groupid'] == 7){
                service("Passport")->logoutLocal();
                $this->error('该帐号还没有通过邮箱验证！');
            }
            //待审核
            if ($userInfo['checked'] == 0) {
                service("Passport")->logoutLocal();
                $this->message(20014, array(), 'error');
            }

            if (cookie('uc_user_synlogin')) {
                $script = uc_user_synlogin($userid);
                cookie('uc_user_synlogin', NULL);
            } else {
                $script = '';
            }

            $data = [
                'userid'   => $userid,
                'username' => $userInfo['username'],
                'modelid'  => $userInfo['modelid'],

            ];
            $this->message("success", [
                'error' => 10000,
                'userInfo' => $data,
            ]);
        } else {
            //登陆失败
            $this->message(20023, array(), 'error');
        }
    }

    /*根据id查询岗位详情
    *@id  岗位id
     * return json
     *
     */
    public function jobshow()
    {
        $id = I('post.id',0,'intval');
        if (!$id)
            $this->message('id参数错误', array(), 'error');
        $model = M('member_firmdata_job');
        $job = $model->where(['id'=>$id])->find();
        if(!$job)
            $this->message('此岗位不存在', array(), 'error');

        if($job['islock'] != 1)
            $this->message('此岗位不公开', array(), 'error');

        $job['j_job_form'] = category_post($job['j_job_form']);
        $job['j_workplace'] = city($job['j_workplace']);
        $job['j_job_type'] = category_jobs($job['j_job_type']);
        $job['j_education'] = category_jobs($job['j_education']);
        $job['j_drivedate'] = category_jobs($job['j_drivedate']);
        $job['j_payscope'] = category_jobs($job['j_payscope']);
        $job['j_drive'] = category_jobs($job['j_drive']);
        if ($job['userid'] > 0) {
            $job['company'] = M('member_company')->where(['userid'=>$job['userid']])->field('userid,f_name,f_nayure,f_scale')->find();
            $job['company']['f_nayure'] = category_jobs($job['company']['f_nayure']); //公司性质
            $job['company']['f_scale'] = category_jobs($job['company']['f_scale']); //公司规模
        }
        $this->message("success", [
            'error' => 10000,
            'data' => $job,
        ]);
    }

    /*公司详情
    *@userid 公司userid
    */
    public function companyshow()
    {
        $userid = I('post.userid','','intval');
        $limit = I('post.limit','2','intval');
        if (!$userid)
            $this->message('userid参数错误', array(), 'error');
        if (M('member')->where(['userid'=>$userid])->find() == false)
            $this->message('没有找到该用户信息', array(), 'error');
        $com = M('member_company')->where(['userid'=>$userid])->field('userid,f_name,f_scale,f_nayure,f_setupdate,f_abstract,f_firmperson,f_phone,f_fax,f_firmurl,f_location,f_coordinate')->find();
        $com['f_scale'] = category_jobs($com['f_scale']);
        $com['f_nayure'] = category_jobs($com['f_nayure']);

        $lists =  M('member_firmdata_job')->field('id,j_postname,j_job_form,j_workplace,j_drivedate,j_payscope,createdate,updatedate')->where(['islock'=>1,'userid'=>$userid])->limit($limit)->select();
        foreach ($lists as $key=>$value){
            $list[$key] = $value;
            $list[$key]['j_job_form'] = category_post($value['j_job_form']);
            $list[$key]['j_workplace'] = city($value['j_workplace']);
//            $list[$key]['j_job_type'] = category_jobs($value['j_job_type']);
//            $list[$key]['j_education'] = category_jobs($value['j_education']);
            $list[$key]['j_drivedate'] = category_jobs($value['j_drivedate']);
            $list[$key]['j_payscope'] = category_jobs($value['j_payscope']);
//            $list[$key]['j_drive'] = category_jobs($value['j_drive']);
        }
        $com['job'] = $list;
        $this->message("success", [
            'error' => 10000,
            'data' => $com,
        ]);
    }

    /*简历管理
    *@userid  用户的id
     */
    public function resume()
    {
        $userid = I('post.userid','','intval');
        if (!$userid)
            $this->message('userid参数错误', array(), 'error');
        if (M('member')->where(['userid'=>$userid])->find() == false)
            $this->message('没有找到该用户信息', array(), 'error');
        $model = M('member_personage');
        $resume = $model->where(['userid'=>$userid])->field('id,userid,title,p_name,updatedate,islock,views')->find();
        $info  = $resume ? 'success' :   '简历还没有被创建';

        $this->message($info, [
            'error' => 10000,
            'data' => $resume,
        ]);

    }
    /*简历预览
    *@id  简历id
     */
    public function resumeshow()
    {
        $id = I('post.id',0,'intval');
        if (!$id)
            $this->message('id参数错误', array(), 'error');
        $resume = M('member_personage')->where(['id'=>$id])->find();
        if (!$resume)
            $this->message('简历不存在', array(), 'error');

        $resume['p_politicsstatus'] =['name'=>category_jobs($resume['p_politicsstatus'] ),'id'=>$resume['p_politicsstatus']];
        $resume['p_nowlocation'] = ['name'=>city($resume['p_nowlocation'] ),'id'=>$resume['p_nowlocation']];
        $resume['p_censusregister'] = ['name'=>city($resume['p_censusregister'] ),'id'=>$resume['p_censusregister']];
        $resume['p_education'] = ['name'=>category_jobs($resume['p_education'] ),'id'=>$resume['p_education']];
        $resume['p_education_undergo'] = unserialize($resume['p_education_undergo'])==false?[]:array_values(unserialize($resume['p_education_undergo']));
        $resume['p_jobdate'] = ['name'=>category_jobs($resume['p_jobdate'] ),'id'=>$resume['p_jobdate']];
        $resume['p_job_undergo'] = unserialize($resume['p_job_undergo'] )==false?[]:array_values(unserialize($resume['p_job_undergo']) );
        $resume['p_drive'] = ['name'=>category_jobs($resume['p_drive'] ),'id'=>$resume['p_drive']];
        $resume['p_job_type'] = ['name'=>category_jobs($resume['p_job_type'] ),'id'=>$resume['p_job_type']];
        $resume['p_job_post'] = ['name'=>category_post($resume['p_job_post'] ),'id'=>$resume['p_job_post']];
        $resume['p_payscope'] = ['name'=>category_jobs($resume['p_payscope'] ),'id'=>$resume['p_payscope']];
        $resume['p_workplace'] = ['name'=>city($resume['p_workplace'] ),'id'=>$resume['p_workplace']];
        $resume['p_comedate'] = ['name'=>category_jobs($resume['p_comedate'] ),'id'=>$resume['p_comedate']];
        unset($resume['p_drivedate']);
        $this->message('success', [
            'error' => 10000,
            'data' => $resume,
        ]);
    }

    /*投递简历
    *@userid 司机用户id
     *@jobid 职位id
     *
     */
    public function jobapply()
    {
        $userid = I('post.userid','','intval');
        $jobid = I('post.jobid','','intval');
        if (!$userid)
            $this->message('userid参数错误', array(), 'error');
        if (M('member')->where(['userid'=>$userid])->find() == false)
            $this->message('userid不存在', array(), 'error');
        //查找该用户的简历
        $mpid = M('member_personage')->where(['userid'=>$userid])->getField('id');
        if (!$mpid)
            $this->message('亲，您还没有创建简历！', array(), 'error');

        //查询公司id
        $comid = M('member_firmdata_job')->where(['id'=>$jobid])->getField('userid');
        if (!$comid)
            $this->message('没有找到岗位所属公司', array(), 'error');

        if( M('jobapply')->where(['userid'=>$userid,'jobid'=>$jobid])->find() )
            $this->message('亲，该职位你已经投过简历了！', array(), 'error');
        $data = [
            'userid' => $userid,
            'jobid'  => $jobid,
            'mpid'   => $mpid,
            'comid'  => $comid
        ];
        $data['id'] = M('jobapply')->add($data);
        if ($data['id']) {
            $this->message('success', [
                'error' => 10000,
                'data' => $data,
            ]);
        } else {
            $this->message('投递失败', array(), 'error');
        }
    }

    /**
     *简历投递记录
     * @userid
     */
    public function jobapplylog()
    {
        $userid = I('post.userid','','intval');
        if (!$userid)
            $this->message('userid参数错误', array(), 'error');
        if (M('member')->where(['userid'=>$userid])->find() == false)
            $this->message('userid不存在', array(), 'error');

        $page = I('post.page','1','intval');
        $size = I('post.size','10','intval');
        $model = M('jobapply');
        $where = ['userid'=>$userid];
        $count = $model->where($where)->count();
        $page = $this->page($count, $size,$page);
        $lists = $model->where($where)->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($lists as $key => $value) {
            $list[$key] = $value;
            $list[$key]['job'] = M('member_firmdata_job')->where(['id'=>$value['jobid']])->field('j_postname,j_payscope,j_workplace,j_drivedate')->find();
            $list[$key]['job']['j_payscope'] = category_jobs($list[$key]['job']['j_payscope']);
            $list[$key]['job']['j_workplace'] = city($list[$key]['job']['j_workplace'] );
            $list[$key]['job']['j_drivedate'] = category_jobs($list[$key]['job']['j_drivedate'] );

        }
        $data['list'] = $list;

        $data['count'] = $count;
        $this->message('success', [
            'error' => 10000,
            'data' => $data,
        ]);
    }
}